package leetcode.code1574;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int findLengthOfShortestSubarray(int[] arr) {
		int len = arr.length;
		int r = 1;
		while (r < len && arr[r] >= arr[r - 1]) {
			r++;
		}
		if (r == len) {
			return 0;
		}
		int l = len - 2;
		while (arr[l] <= arr[l + 1]) {
			l--;
		}
		int ans = Integer.MAX_VALUE;
		for (int i = l + 1, j = 0; i < arr.length; i++) {
			for (; j < r && arr[j] <= arr[i]; j++) {

			}
			ans = Math.min(ans, i - j);
		}
		ans = Math.min(ans, Math.min(len - r, l + 1));
		return ans;
	}

	@Override
	public void debug4() {
		H.print(H.str2array3("[2,2,2,1,1,1]"));
		H.compare(3, this.findLengthOfShortestSubarray(H.str2array3("[2,2,2,1,1,1]")));
		H.print(H.str2array3("[1,2,3,3,10,1,3,3,5]"));
		H.compare(2, this.findLengthOfShortestSubarray(H.str2array3("[1,2,3,3,10,1,3,3,5]")));
		H.compare(3, this.findLengthOfShortestSubarray(H.str2array3("[1,2,3,10,4,2,3,5]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
